﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FileArchive
{
    public partial class DocumentForm : Form
    {
        public DocumentForm()
        {
            InitializeComponent();
            docView.ViewMode = ViewMode.BestFit;
            Width = Screen.PrimaryScreen.Bounds.Width *4/5;
            Height = Screen.PrimaryScreen.Bounds.Height * 4/5;
        }
        private Document doc;
        private int docPos = 0;
        public int DocumentPosition { get { return docPos; }
            set
            {
                var fileNode = (Folder.Children[value] as FileNode);
                if (fileNode != null)
                {
                    docPos = value;
                    if (doc != null)
                        doc.Dispose();
                    doc = new Document(Archive.LoadFile(fileNode));
                    docView.SetDocument(doc);
                    Text = string.Format("Image Viewer - {0}", fileNode.Name);
                }
            }
        }
        internal FolderNode Folder { get; set; }
        internal Archive Archive { get; set; }

        private void MoveNext()
        {
            if (Folder != null)
            {
                int oldDocPos = docPos;
                do
                {
                    docPos++;
                    docPos %= Folder.Children.Count;
                    if (docPos == oldDocPos)
                        break;
                }
                while (!(Folder.Children[docPos] is FileNode) || !ImageExtensions.Contains(Path.GetExtension(Folder.Children[docPos].Name).ToLower()));
                DocumentPosition = docPos;
            }
        }

        private void MovePrevious()
        {
            if (Folder != null)
            {
                int oldDocPos = docPos;
                do
                {
                    docPos--;
                    if (docPos < 0)
                        docPos += Folder.Children.Count;
                    docPos %= Folder.Children.Count;
                    if (docPos == oldDocPos)
                        break;
                }
                while (!(Folder.Children[docPos] is FileNode) || !ImageExtensions.Contains(Path.GetExtension(Folder.Children[docPos].Name).ToLower()));
                DocumentPosition = docPos;
            }
        }

        private void previousButton_Click(object sender, EventArgs e)
        {
            MovePrevious();
        }

        private void nextButton_Click(object sender, EventArgs e)
        {
            MoveNext();
        }

        private void bestFitButton_Click(object sender, EventArgs e)
        {
            docView.ViewMode = ViewMode.BestFit;
        }

        private void actualSizeButton_Click(object sender, EventArgs e)
        {
            docView.ViewMode = ViewMode.OriginalSize;
        }
    }
}
